<template>
  <div class="app-container">

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add()"
                 :disabled="$hasBP('bnt.sysDept.add')  === false"
      >添 加
      </el-button>
      <el-button type="danger" icon="el-icon-delete" size="mini" @click="batchRemove()"
                 :disabled="$hasBP('bnt.sysDept.remove')  === false"
      >批量删除
      </el-button>
    </div>
    <el-table :data="sysDeptList" style="width: 100%;margin-bottom: 20px;margin-top: 10px;" row-key="id" border
              :default-expand-all="false" :tree-props="{ children: 'children' }"
              @selection-change="handleSelectionChange"
    >

      <el-table-column type="selection"/>
      <el-table-column prop="name" label="部门名称"/>
      <el-table-column prop="leader" label="部门负责人"/>
      <el-table-column prop="phone" label="电话"/>
      <el-table-column prop="sortValue" label="排序" width="60"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">

          <el-switch v-model="scope.row.status === 1" :disabled="scope.row.children.length > 0"
                     @change="switchStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" v-if="scope.row.type !== 2" icon="el-icon-plus" size="mini" @click="add(scope.row)"
                     title="添加下级节点" :disabled="$hasBP('bnt.sysMenu.add')  === false"
          />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)" title="修改"
                     :disabled="$hasBP('bnt.sysDept.update')  === false"
          />
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"
                     :disabled="scope.row.children.length > 0"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :rules="formRules" :model="sysDept" label-width="150px" size="small"
               style="padding-right: 40px;"
      >
        <el-form-item label="上级部门" v-if="sysDept.id === ''">
          <el-input v-model="sysDept.parentName" disabled="true"/>
        </el-form-item>

        <el-form-item label="部门名称" prop="name">
          <el-input v-model="sysDept.name" placeholder="请输入部门名称"/>
        </el-form-item>

        <el-form-item label="部门负责人" prop="leader">
          <el-input v-model="sysDept.leader" placeholder="请输入部门负责人"/>
        </el-form-item>

        <el-form-item label="排序">
          <el-input-number v-model="sysDept.sortValue" controls-position="right" :min="0"/>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="sysDept.phone" placeholder="请输入手机号"/>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="sysDept.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api/system/dept'

const defaultForm = {
  id: '',
  parentId: '',
  name: '',
  type: 0,
  treePath: '',
  phone: '',
  leader: '',
  icon: '',
  sortValue: 1,
  status: 1
}
export default {
  // 定义数据
  data() {
    return {
      formRules: {
        name: [{ required: true, trigger: 'blur', message: '请输入部门名称' }],
        leader: [{ required: true, trigger: 'blur', message: '请输入部门负责人' }],
        phone: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        status: [{ required: true, trigger: 'change', message: '请选择状态' }]
      },
      sysDeptList: [],
      expandKeys: [], // 需要自动展开的项

      typeDisabled: false,
      type0Disabled: false,
      type1Disabled: false,
      type2Disabled: false,
      dialogTitle: '',

      dialogVisible: false,
      sysDept: {},
      saveBtnDisabled: false
    }
  },

  //当页面加载时获取数据
  created() {
    this.fetchData()
  },

  methods: {
    //调用api层获取数据库中的数据
    fetchData() {
      console.log('加载列表')
      api.findNodes().then(response => {
        this.sysDeptList = response.data
        console.log(this.sysDeptList)
      })
    },

    //根据id删除数据
    removeDataById(id) {
      // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        return api.removeById(id)
      }).then((response) => {
        this.fetchData()
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      })
    },

    // 复选框发生变化
    handleSelectionChange(selection) {
      this.selectValue = selection
      // console.log(this.selectValue);
    },

    // 批量删除
    batchRemove() {
      if (this.selectValue.length === 0) {
        //提示
        this.$message({
          type: 'warning',
          message: '请选择需要删除的数据!'
        })
        return
      }

      // 进行批量删除
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var idList = []
        // 获取多个复选框对应的id，封装到数组里
        for (var i = 0; i < this.selectValue.length; i++) {
          idList.push(this.selectValue[i].id)
        }

        // 调用方法删除
        api.batchRemove(idList).then(response => {
          //提示信息
          this.$message({
            type: 'success',
            message: '删除成功!'
          })

          // 查询所有数据
          this.fetchData()
        })
      })
    },

    //弹出添加或更新的表单
    add(row) {
      debugger
      this.typeDisabled = false
      this.dialogTitle = '添加下级节点'
      this.dialogVisible = true

      this.sysDept = Object.assign({}, defaultForm)
      this.sysDept.id = ''
      if (row) {
        this.sysDept.parentId = row.id
        this.sysDept.parentName = row.name
        if (row.type === 0) {
          this.sysDept.type = 1
          this.typeDisabled = false
          this.type0Disabled = false
          this.type1Disabled = false
          this.type2Disabled = true
        } else if (row.type === 1) {
          this.sysDept.type = 2
          this.typeDisabled = true
        }
      } else {
        this.dialogTitle = '添加目录节点'
        this.sysDept.type = 0
        this.sysDept.phone = ''
        this.sysDept.parentId = 0
        this.typeDisabled = true
      }
    },

    //编辑
    edit(row) {
      debugger
      this.dialogTitle = '修改部门'
      this.dialogVisible = true
      this.sysDept = Object.assign({}, row)
      this.typeDisabled = true
    },

    //添加或更新
    saveOrUpdate() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.saveBtnDisabled = true // 防止表单重复提交
          if (!this.sysDept.id) {
            this.save()
          } else {
            this.update()
          }
        }
      })
    },

    //添加
    save() {
      api.save(this.sysDept).then(response => {
        this.$message.success('添加成功')
        this.dialogVisible = false
        this.fetchData(this.page)
      })
    },

    //更新
    update() {
      api.updateById(this.sysDept).then(response => {
        this.$message.success('修改成功')
        this.dialogVisible = false
        this.fetchData()
      })
    },

    //修改状态
    switchStatus(row) {
      // 判断当前状态是可用还是禁用
      row.status = row.status === 1 ? 0 : 1

      api.updateStatus(row.id, row.status).then(response => {
        if (row.status === 1) {
          this.$message.success('已启用')
        } else {
          this.$message.warning('已禁用')
        }

        // 查询所有数据
        this.fetchData()
      })
    }

  }
}
</script>
